# -*- coding: utf-8 -*-
"""
@Time ： 2022/7/21 20:19
@Auth ： Mr. William 1052949192
@Company ：特斯汀学院 @testingedu.com.cn
@Function ：函数，字符串替换第n个
"""


def my_replace(s, n, old, new):
    """替换第n个子串"""
    # 先替换前n个
    r_s = s.replace(old, new, n)
    # 再把前n-1个替换回去
    r_s = r_s.replace(new, old, n - 1)
    return r_s


s = 'willroytufeizhangxinwillroytufeizhangxinwillroytufeizhangxin' \
    'willroytufeizhangxin'

s = my_replace(s, 4, 'will', '卡卡')
print(s)
